SHELL=/usr/bin/env bash
PRE_COMMIT_CONFIG_PATH = .pre-commit-config.yaml
TMP_DIR = tmp/

# ANSI color codes
GREEN=$(shell tput -Txterm setaf 2)
YELLOW=$(shell tput -Txterm setaf 3)
RED=$(shell tput -Txterm setaf 1)
BLUE=$(shell tput -Txterm setaf 6)
RESET=$(shell tput -Txterm sgr0)


run:
	@echo "$(YELLOW)Running the application...$(RESET)"
	@source /opt/ros/humble/setup.bash
	@source install/setup.bash
	@ros2 launch turn_on_bichon turn_on_bichon.launch.py

build-and-run:
	@colcon build --packages-select turn_on_bichon bichon_urdf --allow-overriding turn_on_bichon bichon_urdf
	@source install/setup.bash
	@ros2 launch turn_on_bichon turn_on_bichon.launch.py

# Build
build:
	@echo "$(GREEN)Building project...$(RESET)"
	@source /opt/ros/humble/setup.bash
	@$(MAKE) -s check-dependencies
	@$(MAKE) -s install-dependencies
	@colcon build  --parallel-workers 10 --packages-skip tf2_tools nav2_map_server bichon_rrt web_video_server
	@source install/setup.bash \
		colcon build --packages-select bichon_rrt
	@echo "$(GREEN)Build completed successfully.$(RESET)"

install:
	@echo "$(GREEN)Building project...$(RESET)"
	@source /opt/ros/humble/setup.bash \
		colcon build --parallel-workers 6 --packages-skip tf2_tools nav2_map_server bichon_rrt\
		source install/setup.bash \
		colcon build --packages-select bichon_rrt
	@echo "$(GREEN)Build completed successfully.$(RESET)"

build-arm64:
	@echo "$(GREEN)Building project for arm64...$(RESET)"
	@source /opt/ros/humble/setup.bash
	@colcon build --parallel-workers 4  --build-base=./arm64_build --install-base=./arm64_install --mixin arm64
	@source install/setup.bash \
		colcon build --packages-select bichon_rrt \
		colcon build --packages-select bichon_rrt
	@echo "$(GREEN)Build completed successfully.$(RESET)"

check-dependencies:
	@echo "$(YELLOW)Checking dependencies...$(RESET)"
	@source /opt/ros/humble/setup.bash
	@$(MAKE) -s check-system
	@echo "$(GREEN)Dependencies checked successfully.$(RESET)"

check-system:
	@echo "$(YELLOW)Checking system...$(RESET)"
	@if [ "$(shell uname)" = "Darwin" ]; then \
		echo "$(BLUE)macOS detected.$(RESET)"; \
	elif [ "$(shell uname)" = "Linux" ]; then \
		if [ -f "/etc/manjaro-release" ]; then \
			echo "$(BLUE)Manjaro Linux detected.$(RESET)"; \
		else \
			echo "$(BLUE)Linux detected.$(RESET)"; \
		fi; \
	elif [ "$$(uname -r | grep -i microsoft)" ]; then \
		echo "$(BLUE)Windows Subsystem for Linux detected.$(RESET)"; \
	else \
		echo "$(RED)Unsupported system detected. Please use macOS, Linux, or Windows Subsystem for Linux (WSL).$(RESET)"; \
		exit 1; \
	fi

install-dependencies:
	@echo "$(YELLOW)Installing dependencies...$(RESET)"
	@$(MAKE) -s install-pre-commit-hooks
	@echo "$(GREEN)Dependencies installed successfully.$(RESET)"

install-pre-commit-hooks:
	@echo "$(YELLOW)Installing pre-commit hooks...$(RESET)"
	@git config --unset-all core.hooksPath || true
	@pip install --user pre-commit
	@echo "$(GREEN)Pre-commit hooks installed successfully.$(RESET)"

lint:
	@echo "$(YELLOW)Running linters...$(RESET)"
	@pre-commit run --files src/**/* --show-diff-on-failure --config $(PRE_COMMIT_CONFIG_PATH)

test:
	@source /opt/ros/humble/setup.bash
	@colcon test
	@colcon test-result --verbose

# Run the app (standard mode)
run:
	@mkdir -p logs
	@echo "$(YELLOW)Waiting for the backend to start...$(RESET)"
	@source /opt/ros/humble/setup.bash
	@ros2 launch turn_on_bichon bichon_lidar.launch.py
	@ros2 launch turn_on_bichon turn_on_bichon.launch.py
	@echo "$(GREEN)Backend started successfully.$(RESET)"

# Clean up all caches
clean:
	@echo "$(YELLOW)Cleaning up caches...$(RESET)"
	@rm -rf log
	@rm -rf build
	@rm -rf install
	@rm -rf $(TMP_DIR)
	@echo "$(GREEN)Caches cleaned up successfully.$(RESET)"

# Help
help:
	@echo "$(BLUE)Usage: make [target]$(RESET)"
	@echo "Targets:"
	@echo "  $(GREEN)build$(RESET)               - Build the entire project (includes environment setup and dependencies)."
	@echo "  $(GREEN)check-dependencies$(RESET) - Verify system dependencies are installed."
	@echo "  $(GREEN)clean$(RESET)             - Remove all generated files (build/, install/, logs/)."
	@echo "  $(GREEN)install-dependencies$(RESET) - Install required dependencies (pre-commit hooks, ROS packages, etc.)."
	@echo "  $(GREEN)lint$(RESET)              - Run linters (configured via .pre-commit-config.yaml)."
	@echo "  $(GREEN)run$(RESET)               - Launch the application (ensure built first; logs saved in 'logs/')."
	@echo "  $(GREEN)test$(RESET)              - Run tests and display results verbosely."
	@echo "  $(GREEN)help$(RESET)              - Show this help message with target details."
